package com.wu.common.util

/**
 * 字符串转换格式工具
 *
 * @author zhangrq
 */
@Suppress("unused")
object NumberFormatUtils {

    private val TAG = NumberFormatUtils::class.java.simpleName

    /**
     * 字符串在转换成int值
     */
    @JvmStatic
    fun toInt(value: String?): Int {
        return value?.toIntOrNull() ?: 0
    }

    /**
     * 字符串在转换成int值
     */
    @JvmStatic
    fun toInt(value: String?, defaultValue: Int): Int {
        return value?.toIntOrNull() ?: defaultValue
    }

    /**
     * 字符串在转换成double值
     */
    @JvmStatic
    fun toDouble(value: String?): Double {
        return value?.toDoubleOrNull() ?: 0.0
    }

    /**
     * 字符串在转换成double值
     */
    @JvmStatic
    fun toDouble(value: String?, defaultValue: Double): Double {
        return value?.toDoubleOrNull() ?: defaultValue
    }

    /**
     * 字符串在转换成long值
     */
    @JvmStatic
    fun toLong(value: String?): Long {
        return value?.toLongOrNull() ?: 0L
    }

    /**
     * 字符串在转换成long值
     */
    @JvmStatic
    fun toLong(value: String?, defaultValue: Long): Long {
        return value?.toLongOrNull() ?: defaultValue
    }

    /**
     * 字符串在转换成float值
     */
    @JvmStatic
    fun toFloat(value: String?): Float {
        return value?.toFloatOrNull() ?: 0F
    }

    /**
     * 字符串在转换成float值
     */
    @JvmStatic
    fun toFloat(value: String?, defaultValue: Float): Float {
        return value?.toFloatOrNull() ?: defaultValue
    }

    /**
     * 字符串在转换成byte值
     */
    @JvmStatic
    fun toByte(value: String?): Byte {
        return value?.toByteOrNull() ?: 0
    }

    /**
     * 字符串在转换成byte值
     */
    @JvmStatic
    fun toByte(value: String?, defaultValue: Byte): Byte {
        return value?.toByteOrNull() ?: defaultValue
    }

    /**
     * 字符串在转换成short值
     */
    @JvmStatic
    fun toShort(value: String?): Short {
        return value?.toShortOrNull() ?: 0
    }

    /**
     * 字符串在转换成short值
     */
    @JvmStatic
    fun toShort(value: String?, defaultValue: Short): Short {
        return value?.toShortOrNull() ?: defaultValue
    }
}
